Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
@shopify/useful-types
Advanced tools
@shopify/useful-types
A few handy TypeScript types.
$ yarn add @shopify/useful-types
The following type aliases are provided by this library:
ThenType<T>
: When T
is a promise, resolves to the type the promise will resolve to (Promise<infer U>
). Otherwise, resolves to T
. Useful when you may or may not have a promise and need to reference the underlying type.
const value = 'foo';
const promise = Promise.resolve(value);
type V = ThenType<typeof value>; // string
type P = ThenType<typeof promise>; // string
ArgumentAtIndex<T, Index>
: Resolves to the type of the argument of the passed function at the passed index. Useful for cases where you wish to extract the type of arguments without actually exporting the argument types, and is a nice complement to TypeScript’s built-in ReturnType
.
const func = (foo: Promise<any>, bar: string) => foo.then(() => bar);
type Arg1 = ArgumentAtIndex<typeof func, 0>; // Promise<any>
type Arg2 = ArgumentAtIndex<typeof func, 1>; // string
type NotAnArg = ArgumentAtIndex<string, 0>; // never
FirstArgument<T>
: Resolves to the type of the first argument to the passed function. This is shorthand for ArgumentAtIndex<T, 0>
.
const func = (foo: Promise<any>) => foo.then(() => 'bar');
type Arg = FirstArgument<typeof func>; // Promise<any>
ArrayElement<T>
: When T
is an array, resolves to the type contained within the array.
type FooArray = (string | number)[];
type Foo = ArrayElement<FooArray>; // string | number
Omit<T, K extends keyof T>
: The opposite of TypeScript’s Pick
type. Resolves to a new type that includes all keys in the original except those matching K
.
interface Obj {
foo: string;
bar: boolean;
baz: number;
}
type SelectiveObj = Omit<Obj, 'foo' | 'bar'>; // {baz: number}
Props<T>
: Extracts the prop type from a React component. This allows you to access property types without having to manually export/ import the type.
function MyComponent({name}: {name: string}) {
return <div>Hello, {name}!</div>;
}
class MyOtherComponent extends React.Component<{seconds: number}> {
render() {
return <div>{this.props.seconds} seconds left!</div>;
}
}
type MyComponentProps = Props<typeof MyComponent>; // {name: string}
type MyOtherComponentProps = Props<typeof MyOtherComponent>; // {seconds: number}
DeepPartial<T>
: Recusively maps over all properties in a type and transforms them to be optional. Useful when you need to make optional all of the properties (and nested properties) of an existing type.
interface Obj {
foo: string;
bar: {
baz: boolean;
};
}
type DeepPartialObj = DeepPartial<Obj>; // {foo?: string; bar?: { baz?: boolean }}
FAQs
A few handy TypeScript types
The npm package @shopify/useful-types receives a total of 114,219 weekly downloads. As such, @shopify/useful-types popularity was classified as popular.
We found that @shopify/useful-types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 24 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.